package com.lanou.sfu.songforu.music_library_fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.lanou.sfu.songforu.R;
import com.lanou.sfu.songforu.adapter.ArtistDetailAlbumAdapter;
import com.lanou.sfu.songforu.base.BaseFragment;
import com.lanou.sfu.songforu.constant.Url;
import com.lanou.sfu.songforu.entity.ArtistDetailAlbumEntity;
import com.lanou.sfu.songforu.singleton.VolleySingleton;

import java.util.List;

/**
 * Created by dllo on 15/10/28.
 */
public class ArtistDetailAlbumFragment extends BaseFragment {
    private View v;
    private VolleySingleton requestQueue;
    private Context context;
    private ListView listView;
    private StringRequest request;
    private List<ArtistDetailAlbumEntity.AlbumlistEntity> entityList;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        v = inflater.inflate(R.layout.fragment_artist_detail_album, null);
        context = getActivity();
        listView = (ListView) v.findViewById(R.id.artist_detail_album_listview);
        requestQueue = VolleySingleton.getInstance(context);
        return v;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        final Bundle bundle = getArguments();
        String id = bundle.getString(context.getString(R.string.id));
        String url = Url.ALBUM_ID + id;
        request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                ArtistDetailAlbumEntity albumEntity = gson.fromJson(response,ArtistDetailAlbumEntity.class);
                entityList = albumEntity.getAlbumlist();
                ArtistDetailAlbumAdapter adapter = new ArtistDetailAlbumAdapter(context,entityList);
                listView.setAdapter(adapter);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        requestQueue.addQueue(request);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ArtistAlbumFragment fragment = new ArtistAlbumFragment();
                Bundle bundle1 = new Bundle();
                bundle1.putString(getActivity().getString(R.string.album_id), entityList.get(position).getAlbum_id());
                fragment.setArguments(bundle1);
                getActivity().getSupportFragmentManager().beginTransaction().addToBackStack(null).replace(R.id.fl_main,fragment).commit();

            }
        });

    }
}
